
package View;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.JPanel;

import BaseObject.Motor;
import BaseObject.Taxi;

@SuppressWarnings("serial")
public class SimulationInterface extends JPanel{

	private static int rayCircle;
	//private Taxi taxi;// we suppose that we've got only a taxi here,this will be changed into a listTaxi when Class Motor has been completed
	private double scale;
	private Motor motor;
	
	
	public static void main(String[] args) 
	{

		Motor motor = new Motor();
		motor.initialize();
		
		
		for(int i=0;i<Motor.nbTaxis;i++)
		{
			Motor.taxis.add(new Taxi());
		}
		

		SimulationInterface s = new SimulationInterface(motor);
		
		while(!motor.StopTheSystem()){
			try {
				Thread.sleep(100);
				s.repaint();
			
				motor.updateClients();
				motor.updateTaxis();
				motor.increaseCurrentTime();
			
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		System.out.println("-----RECAPITULATIF------------");
		System.out.println("Nombre de client produit : " + motor.getNbClientProduced());
		System.out.println("Nombre de client satisfait : " + motor.getNbClientDone());
		System.out.println("Nombre de client non Satisfait : " + motor.getNbClientMissed());
		System.out.println("FIN DU PROGRAMME");
		
		
		
	}

	/*variable Taxi will be deleted*/
	public SimulationInterface(Motor motor){
		rayCircle = getRayDimension();
		JFrame frame = new JFrame("Interface Of Simulation Numerique");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(this);
		frame.setSize(2*rayCircle+30,2*rayCircle+50);
		frame.setVisible(true);  
		frame.setLocationRelativeTo(frame.getParent());
		
		this.scale = getRayDimension()/50;
		this.motor = motor;
		
	}
	
	public int getRayDimension(){
		Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		return ((int)screenSize.getHeight()/3);
	}

	//public class MyComponent extends JComponent{
	public void paint(Graphics g){
		
		//Clear(g);
		super.paintComponent(g);
		Graphics2D g2d =(Graphics2D)g;
		
		g2d.setColor(Color.red);
		Ellipse2D.Double circle= new Ellipse2D.Double(0,0,2*rayCircle,2*rayCircle);
		Ellipse2D.Double centreCircle= new Ellipse2D.Double(0+rayCircle-this.scale/2,0+rayCircle-this.scale/2,this.scale,this.scale);
		g2d.fill(centreCircle);
		g2d.draw(circle);

		
		g2d.setColor(Color.blue);
		
		Iterator<Taxi> itTaxi = Motor.getTaxis().iterator();

		while(itTaxi.hasNext())
		{
			Taxi taxi = itTaxi.next();
		
			Ellipse2D.Double taxiCircle= new Ellipse2D.Double(taxi.getCurrentPositionTaxi().getX()*this.scale+rayCircle-this.scale/2,taxi.getCurrentPositionTaxi().getY()*this.scale+rayCircle-this.scale/2,this.scale,this.scale);
			
			g2d.drawString(""+taxi.getTekenClientList().size()+"", (float)((taxi.getCurrentPositionTaxi().getX()*this.scale+rayCircle-this.scale/2)), (float)(taxi.getCurrentPositionTaxi().getY()*this.scale+rayCircle-this.scale/2));

			g2d.fill(taxiCircle);
			
			g2d.setColor(Color.GREEN);
			
			for(int i=0;i<taxi.getTekenClientList().size();i++)
			{
				
					double x =taxi.getTekenClientList().get(i).getDestination().getX()*this.scale;
					double y= taxi.getTekenClientList().get(i).getDestination().getY()*this.scale;
					Ellipse2D.Double destination=new Ellipse2D.Double(x+rayCircle-this.scale/2,y+rayCircle-this.scale/2,this.scale,this.scale);
					Line2D.Double trace = new Line2D.Double(taxi.getCurrentPositionTaxi().getX()*this.scale+rayCircle,taxi.getCurrentPositionTaxi().getY()*this.scale+rayCircle, (float)((x+rayCircle)),(float)(y+rayCircle));
					g2d.fill(destination);
					g2d.draw(trace);
					g2d.drawString("D"+(i+1)+"", (float)((x+rayCircle)), (float)(y+rayCircle));

			}
			g2d.setColor(Color.blue);
			
		}
		
		/*draw clients who are still waiting for a taxi with color orange*/
		g2d.setColor(Color.ORANGE);
		for(int i=0;i<Motor.getClients().size();i++)
		{
			if(Motor.getClients().get(i).getStatus()==0)//only draw clients who are still waiting for a taxi 
			{
				double px =Motor.getClients().get(i).getPosition().getX()*this.scale;
				double py= Motor.getClients().get(i).getPosition().getY()*this.scale;
				double dx =Motor.getClients().get(i).getDestination().getX()*this.scale;
				double dy= Motor.getClients().get(i).getDestination().getY()*this.scale;
				
				Ellipse2D.Double position=new Ellipse2D.Double(px+rayCircle-this.scale/2,py+rayCircle-this.scale/2,this.scale,this.scale);
				Ellipse2D.Double destination=new Ellipse2D.Double(dx+rayCircle-this.scale/2,dy+rayCircle-this.scale/2,this.scale,this.scale);
				
				Line2D.Double trace = new Line2D.Double(px+rayCircle,py+rayCircle, dx+rayCircle,dy+rayCircle);
				
				g2d.fill(position);
				g2d.fill(destination);
				g2d.draw(trace);
				
				//g2d.drawString("0", (float)((px+rayCircle)), (float)(py+rayCircle));
				g2d.drawString("D"+(i+1)+"", (float)((dx+rayCircle)), (float)(dy+rayCircle));

			}
		}
		}
	protected void Clear(Graphics g)
	{
		super.paintComponents(g);
		
	}
}